CN.record = "个学生";
var swfu;
var msg=CN;

swfucallback = function(){//finish upload csv;
	$(".open_swfu").click();
	$(".tablistcsv").empty();
	$(".tablistcsv").append("<tr><td colspan='3'>正在加载csv数据...</td></tr>");
	cb = function(data){
		if(data.code==1){			
			if(data.data.length!=0){
				$(".tablistcsv").empty();
				var td = function(arg,title){
		        		if(!arg)arg="-";
			        	if(title){
			        		return "<td title='"+title+"'>"+arg+"</td>";           
			        	}else{
			        		return "<td>"+arg+"</td>";
			        	}
		            };
		        csvdetail.csvdata = data.data;
				$.each(data.data,function(index,value){
		        	var kk =data.data[index],
		             	tdValue =
		             	td(kk[0])+
		             	td(kk[2])+
		            	td(kk[4]);
		               $(".tablistcsv").append("<tr>"+tdValue+"</tr>");
				});
			}else{
				$(".tablistcsv").empty();
				$(".tablistcsv").append("<tr><td colspan='3'>上传csv文件有误</td></tr>");
			}
		}
	}
	var json = {url:site.web+site.module+"/batchimport",action:"read",filepath:ufile.filepath,gradename:csvdetail.gradename,gid:csvdetail.gid,uid:site.uid};
	G(json,cb);
}

var ufile = {fid:0,filepath:"",name:"",callback:true,fn:swfucallback};
var userfile = {fid:0,selectfileid:0,name:"",path:"",target:""};
var objectModel = {edit_status:"create",uid:site.uid,status:0,sex:1,gradename:"",usertype:1};
var csvdetail = {year:"",gid:0,gradename:"",csvdata:null};
var isselectclass = false;
$(function(){
	init();
	mpage.modalfactory = function(data,position){//生成列表内容 //TODO 需要变化的地方
        $(".tablist").empty();
        tablist.position = position;//设置数据所在位置
        var tdValue =  "",
            rowclass = "",
            td = function(arg,title){
        		if(!arg)arg="-";
	        	if(title){
	        		return "<td title='"+title+"'>"+arg+"</td>";           
	        	}else{
	        		return "<td>"+arg+"</td>";
	        	}
            },
            setStatus = function(arg){
            	var sta = ["未绑定","审核中","已绑定"];
            	var cla = ["","label-important","label-success"];
            	return "<span class='label "+cla[arg]+"'>"+sta[arg]+"</span>";
            };
            setButton = function(data){
            	var btns = '<div class="btn-group" data-toggle="buttons-radio">';
            	btns += "<a class='btn btn-info btn-mini' onclick='del(this)' data-name='"+data.name+"' data-id='"+data.cid+"'>移除</a>";
            	btns += "</div>";
            	return btns;
            };
        for ( var element in data) {
        	var kk =data[element],
             	u_btn = '<button class="btn btn-info btn-mini" onclick="get(this)" type="button" data-id="'+kk.cid+'">'+kk.name+'</button>';
             	tdValue =
             	td(u_btn)+
             	td(kk.gradename)+
             	td(kk.graduation)+
            	td(kk.hometown)+ 
            	td(kk.mobile)+ 
            	td(kk.corpposition)+
            	td(kk.corp)+
            	td(setStatus(kk.status))+
             	td(setButton(kk));
               	rowclass = element%2;//行样式
               $(".tablist").append("<tr class='tr"+rowclass+"'>"+tdValue+"</tr>");
        }
    };
	mpage.init();
}); 



function init(){
	init_dialog();
	init_btn();
	init_swfupload();
}

init_btn = function(){		
	var this_year = new Date().getFullYear();
	var option = "<option class='choose_year'>请选择入学年份</option>";
	while(this_year>1969){
		option += "<option class='choose_year'>"+this_year+"</option>";
		this_year--;
	}	
	$(".select_year").append(option);
	
	$('.select_year').change(function(){ //选择年份
		var key = $(this).children('option:selected').val();//这就是selected的值 
		$(".select_class").show();
		$(".select_class").empty();
		$(".select_class").append("<option value='0'>没有班级信息</option>");
		
		var flag = $(this).attr("key");
		csvdetail.year = key;
		
		if(key=="请选择入学年份")return;
		$(".yearmsg").show();
		
		cb = function(data){
			option = "";			
			if(data.code==1){
				if(data.data.length!=0){	
					$(".select_class").empty();
					$(".select_class").append("<option value='0'>请选择班级</option>");
					$.each(data.data,function(index,value){
						var gid = data.data[index].gid,
							name= data.data[index].name;
						option += "<option value='"+gid+"'>"+name+"</option>";
					});
					$(".select_class").append(option);
				}						
			}			
			
			$(".yearmsg").hide();
			$(".select_class").show();
		}
		var json = {url:site.web+"grade/list",year:key,uid:site.uid};
		G(json,cb)
		}); 
	
	$('.select_class').change(function(){ //选择班级
		isselectclass = true;
		var flag = $(this).attr("key");
		if(flag=="class"){
			var name = $(this).children('option:selected').html(),
				val = $(this).children('option:selected').val();
			//objectModel:set name, set gid
			objectModel.gradename = name;
			objectModel.gid = val;
			if(val==0){
				objectModel.gradename = "";
			}
			$(".gname").val(objectModel.gradename);
		}
		
		if(flag=="csv"){
			var name = $(this).children('option:selected').html(),
				val = $(this).children('option:selected').val();
			$(".csv_classname").val(csvdetail.year+name);
			csvdetail.gid = val;
			csvdetail.gradename = csvdetail.year+name;
			
			//effect			
			$(".tabclasslist").empty();
			$(".tabclasslist").append("<tr><td  colspan='4'>数据加载...</td></tr>");
			//load classmate
			cb = function(data){
				if(data.code==1){
					if(data.data.count!=0){
						$(".tabclasslist").empty();
						var td = function(arg,title){
				        		if(!arg)arg="-";
					        	if(title){
					        		return "<td title='"+title+"'>"+arg+"</td>";           
					        	}else{
					        		return "<td>"+arg+"</td>";
					        	}
				            },
			            setStatus = function(arg){
			            	var sta = ["未绑定","审核中","已绑定"];
			            	var cla = ["","label-important","label-success"];
			            	return "<span class='label "+cla[arg]+"'>"+sta[arg]+"</span>";
			            };
						$.each(data.data.list,function(index,value){
				        	var kk =data.data.list[index],
				             	tdValue =
				             	td(kk.name)+
				             	td(kk.graduation)+
				            	td(kk.mobile)+ 
				            	td(setStatus(kk.status));
				               $(".tabclasslist").append("<tr>"+tdValue+"</tr>");
						});
						
					}else{
						$(".tabclasslist").empty();
						$(".tabclasslist").append("<tr><td  colspan='4'>没有校友数据</td></tr>");
					}
				}
			}
			var json = {url:site.web+site.module+"/getgradeclassmate",uid:site.uid,gid:val};
			G(json,cb);
		}
		$(".select_class").hide();
	}); 
		
	$(".setsex").click(function(){
		var key = $(this).attr("key");
		objectModel.sex = key;
	});
	
	$(".open_swfu").click(function(){
		var key = $(this).attr("key");
		var sign = ["left","right"];
		var flag = key == 0 ? 1 : 0;
		//TODO: 20131012
		var htm = '导入CSV文件<i class="icon-chevron-'+sign[flag]+' i_href margin5lr"></i>';
		$(this).parent().next().toggle(); 			
		$(this).attr("key",flag);
		$(this).html(htm);
	});
}

init_swfupload = function(){
	var settings = getsetting(site);
	settings.file_types = "*.csv";
	swfu = new SWFUpload(settings);
}

init_dialog = function(){
	$("#dialog_create").click(function(){//create & open dialog
		objectModel.edit_status = "create";
		open_dialog();
	});
	
	$("#dialog_input").click(function(){
		clearcsvdialog();
		$("#myCSV").modal("show");
		
	});
	
	$("#dialog_csv_save").click(function(){
		$(".dialogcsvmsg").text("");
		if($(".csv_classname").val()==""||csvdetail.gid==0){
			$(".dialogcsvmsg").text("请选择要导入的班级！");
			return;
		}
		if(!csvdetail.csvdata){
			$(".dialogcsvmsg").text("请导入"+$(".csv_classname").val()+"的CSV数据");
			return;
		}
		//effect
		$(".dialogcsvmsg").text("正在导入数据,请稍后...");
		cb = function(data){
			if(data.code==1){
				$(".dialogcsvmsg").text("导入数据成功!");
				finishcsv = function(){
					$("#myCSV").modal("hide");
					mpage.init();					
				};
				setTimeout('finishcsv( )', 1000);
			}else{
				$(".dialogcsvmsg").text("服务器繁忙,请稍后再试");
			}
		}		
		var json = {url:site.web+site.module+"/batchimport",action:"save",filepath:ufile.filepath,gradename:csvdetail.gradename,gid:csvdetail.gid,uid:site.uid};
		G(json,cb);		
	});
	
	$("#dialog_save").click(function(){
		
		if(!objectModel.gid){
			$(".dialogmsg").text("请选择班级!");
			return;
		}
		
		$(this).attr("disabled",true);
		$(".dialogmsg").text("执行操作中...");
		$("#form2 input").each(function(){
			var id = $(this).attr("id");
			if(id)
				objectModel[id] =  $(this).val();
		});
		if(isselectclass){
			objectModel.gradename = csvdetail.year + $(".gname").val();
		}
		objectModel.uid = site.uid;
		objectModel.url = site.web+site.module+"/"+objectModel.edit_status;//包含create/update操作
		
		complete = function(data){
			done_method = function(){
				$('#myModal').modal('hide');
				mpage.init();
			};
			if(data.code==1){
				$(".dialogmsg").text("操作成功!");
				setTimeout(done_method, 2000);
			}else{
				$('#myModal').modal('hide');
			}
			
		}
		G(objectModel,complete);
	});	
	
	$(".canceldialog").click(function(){//cancel
		clear_dialog();
		$("#"+$(this).attr("key")).modal('hide');
	});
}

clear_dialog = function(){
	$("#form2 input[type=reset]").click();
	$("#form3 input[type=reset]").click();
	$("#dialog_save").attr("disabled",false);
	$(".dialogmsg").text("");
	isselectclass = false;
}

clearcsvdialog = function(){
	$(".csv_classname").val("");
	$(".tabclasslist").empty();
	$(".tablistcsv").empty();
	$(".tablistcsv").append("<tr><td colspan='3'>请上传csv文件</td></tr>");
	csvdetail.gid = 0;
	csvdetail.gradename = "";
}

open_dialog = function(data){
	clear_dialog();
	switch(objectModel.edit_status){
		case "create":
			$(".dialog_status").text("新增");
			break;
		case "update":
			$(".dialog_status").text("更新");

			$.each(data, function(index, value) {
				$("#"+index).val(value);
			});
			objectModel = data;
			objectModel.edit_status = "update";
			$(".gname").val(objectModel.gradename);
			objectModel.uid = site.uid;
			
			$(".setsex").each(function(kev,value){
				if($(this).attr("key")==objectModel.sex){
					$(this).click();
				}
			});
			break;
	}
	$('#myModal').modal('show');
}

get = function(arg){
	var id = $(arg).attr("data-id");
	cb = function (data){
		if(data.code==1){
			objectModel.edit_status = "update";
			open_dialog (data.data);
		}else{
			alert("ERROR grade 404");
		}
	}
	var url = site.web+site.module+"/get";
	var json = {url:url,cid:id,uid:site.uid};
	G(json,cb);
}

del = function(arg){
	var name =  $(arg).attr("data-name");
	if(confirm("确认移除"+name+"?")==false)return;
	var id = $(arg).attr("data-id");
	cb = function (data){
		mpage.init();
	}
	var url = site.web+site.module+"/del";
	var json = {url:url,cid:id,uid:site.uid};
	G(json,cb);
}


